#include "../include/head.h"
#include "../include/func.h"

/* Usage:  */
int main(int argc, char *argv[]){                                  
    ARGS_CHECK(argc, 2);

    char ip[16] = ""; // 15个字符加上'\0'
    char port[6] = ""; // 5个字符加上'\0'
    conf_get(argv[1], ip, port); // 从配置文件中取到服务端ip和port
    socket_t fd_server = tcp_connect(ip, port); // 建立tcp连接,得到通信socketfd
    ERROR_CHECK(fd_server, -1, "tcp_connect");

    // 从标准输入中读取命令，发给服务器
    char buf[128] = {0};
    Cmd_t cmd = {0};
    Train_t train = {0};
    while (1) {
        read(STDIN_FILENO, buf, sizeof(buf)); // 键盘输入命令
        buf[strlen(buf) - 1] = '\0';
        // strcmp函数比较两个字符串，如果相等则返回0
        if (!(strcmp(buf, "quit"))) { // 输入quit时表示退出
            exit(1);
        }

        init_cmd(buf, &cmd); // 将stdin输入的命令分割，存入cmd结构体中, 包含参数个数和命令内容
        int cret = cmd_check(&cmd); // 检查命令是否合法
        if (cret == -1) {
            printf("illegal command!\n");
            continue;
        }
        print_cmd(&cmd); // 打印命令
        memcpy(train._data, &cmd, sizeof(cmd)); // 将命令放入小火车传输内容中

        train._data_len = sizeof(cmd); 
        send(fd_server, &train._data_len, sizeof(int), 0);
        send(fd_server, train._data, train._data_len, 0);

        if (!(strncmp("cd", cmd._cmd[0], 2))) {
            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        } else if (!strncmp("pwd", cmd._cmd[0], 3)) {  
            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        } else if (!strncmp("rm", cmd._cmd[0], 2)) {
            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        } else if (!strncmp("mkdir", cmd._cmd[0], 5)) {
            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        } else if (!strncmp("rmdir", cmd._cmd[0], 5)) {
            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        } else if (!strncmp("ls", cmd._cmd[0], 2)) {
            while (1) {
                recv(fd_server, &train._data_len, sizeof(int), 0);
                if (train._data_len == 0) {
                    break;
                }
                recv(fd_server, train._data, train._data_len, MSG_WAITALL);
                printf("%s\n", train._data);
            }
        }
        else if (!strncmp("puts", cmd._cmd[0], 4)) {
            upload(fd_server, cmd._cmd[1]);

            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);

        } else if (!strncmp("gets", cmd._cmd[0], 4)) {

            download(fd_server, cmd._cmd[1]);

            recv(fd_server, &train._data_len, sizeof(int), 0);
            recv(fd_server, train._data, train._data_len, MSG_WAITALL);
            printf("%s\n", train._data);
        }
    }

    return 0;
}

